package com.jp.knowledge.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.jp.knowledge.R;

public class SquareImageView extends ImageView {

    private boolean sizeWithWidth;
    private boolean sizeWithHeight;

    public SquareImageView(Context context) {
        this(context, null);
    }
    public SquareImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SquareImageView);
        sizeWithWidth = typedArray.getBoolean(R.styleable.SquareImageView_sizeWithWidth, false);
        sizeWithHeight = typedArray.getBoolean(R.styleable.SquareImageView_sizeWithHeight, false);
    }

    public void setSizeWithWidth(boolean sizeWithWidth){
        this.sizeWithWidth = sizeWithWidth;
    }
    public void setSizeWithHeight(boolean sizeWithHeight){
        this.sizeWithHeight = sizeWithHeight;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        if(sizeWithWidth && !sizeWithHeight){
            setMeasuredDimension(sizeWidth, sizeWidth);
        } else if(!sizeWithWidth && sizeWithHeight){
            setMeasuredDimension(sizeHeight, sizeHeight);
        } else{
            setMeasuredDimension(sizeWidth, sizeHeight);
        }
    }
}
